Hello and welcome back to the costs.
So in the last sessions we already got in touch with String objects and now we we dig a bit deeper into
the strings however.
Also later in our real world projects then our focus is not on strings so we we keep it here as hard
as possible.
So at the end of the last session we transformed an integer five into a string by putting it into quotation
marks.
Let's repeat this again.
So we are transforming into a five to a string and this completely changes the functionality of the
object.
So before five plus to give seven.
And now if you execute this string five so represented by C plus two you get an error message.
So it says it must be a string not an integer so it completely changes the functionality.
But what other strings actually.
Well strings up a sequence of Unicode characters inside a quotation mark.
And this is treated like any other sequence like Lists Tuples which we see later.
And yeah actually we can when we can put into a string many different characters like numbers letters
special characters like question marks and exclamation marks and so on which you will see later.
But before now let's start with.
Just um.
Yeah.
Assigning or creating and string object Doc and assign the variable D.
And also we create a string object cat and assign the right E.
And no surprise if we print the types of DNA e both strings and if you tried to add both strings as
before we tried to apply the plus operator let's see what we get here.
So not Python does it just concatenate both strings so D plus E gets dark.
Cat and if we might add E plus D we get cat dog.
So it's just concatenate ing both strings and I yeah if you try to to a substrate you can.
Error message.
So minus the the minus operator is not the defined four strings and if you tried to multiply two strings
we also get them error message.
And if we tried to add uh let's say an integer to a string.
That's awesome.
Error message it doesn't work but if we transform our integer five into a string then of course we can
add this string to another string like a cat or dog so we can talk five and it works.
Also the other way around.
So we get five dog and if you tried to multiply a string with an integer like five we get and string
with a five times the copy of dog so we get talk talk talk talk dog so dark and times five is just concatenated
ing five times talk and we can also do it the other way around and get the same result.
So it doesn't matter if three times five four five times D.
However if you transform the integer five to a string and try to multiply it with our string do you.
Of course we get an error message and also we can concatenate many strings to one string.
So for example we have the string I we the string half string 5 and the string docs and thought we get
the statement I have five docs.
So as you can see here we need the empty spaces here.
If we forget these our sentence or our statement looks a bit a little bit unreadable.
So Python can also handle spaces here and spaces can be part of a string so that s s quite comfortable
here.
What I would like to introduce here is string formatting.
So we are assigning um the string docs to the variety of animal and the integer 6 to amount and then
we print the statement.
I have curly brackets curly brackets dot end of the quotes and then I apply it to the string the method
format and insides of the brackets a format I'm I'm defining for which the curly brackets are place
holders so undefined execute the cell here I get I have five docs.
So what the method dot format actually does is that the.
Yeah it fits the place holders um in the sequence as defined within the brackets here.
So so the first curly bracket will be replaced by the amount and the second curly bracket will be replaced
by animal.
And uh yeah for example.
So yesterday I had six docs.
Now today I have not docs but cards and not six.
But let's say eight I updated I got I have eight kids and this is quite helpful actually if you had
your variables are changing many times and you want to automate your statement how many cards or talks
you have currently.
So that's that's quite helpful here.
What you can also do you can delete a whole string by calling the DL function.
So inside the brackets you put the object D so D stands for dark and if you deleted so if you then tried
to print the so pretty is not defined here it says.
So now we have to redefine DIA again in order D again OK dark before I mentioned that a string is a
sequence of Unicode characters and um yeah you by calling the line function you can count the numbers
of items in a string so with these we would expect three items d o a..
All right.
And we can also count the number of items or the number of characters and statement it's 40 and so let's
go back to our statement.
So our statement is I have five talks.
So when we count we have to count actually all characters.
So also the space character.
So one two three four five six seven eight nine ten eleven twelve thirteen and also the DOT 14 characters.
So that is correct here.
And before we set different data types have different functionality so we could call the length length
function on strings but we cannot call the length function on integers or floats.
If you tried to do so you get an error message.
So object type Integer has no length function or the length function is not defined and earlier we saw
that integers and floats they have built in methods attributes and this holds also true for String object.
So seeing objects also have predefined appeared in methods and attributes and one of this is uh the
upper method and it's execute the upper method on our String objects statement and that s print statement.
Now you can see all items all our characters are converted to uppercase.
So what actually the upper method does that just.
Yeah.
It converts all characters to the uppercase and we can also call the opposite method.
Dot lower if we do so we get.
I have talked some here we can see all characters are.
Yeah.
Lowercase and s.
Another method called capitalize.
And if we call this method on statement we get I have talks but the.
I hear you see is capitalized.
So the first character in our string is capitalized with them the capitalized method and there are many
more build and methods for String objects.
And if you like you can look at the documentation or just press the dots tab to see what pace and office
here and I can just try out what what happens if you call the the method then then the and attributes
here
but for now we are finished here with the strings and we head over to lists and s some function call
list which tree app transforms a string object into a list so we can try.
What happens here.
So our statement is I have five docs and we transform it into a list and what we can see here.
We have all single characters separated by a comma inside craft bracket brackets and this is called
The List.
So if you check the type type l This is a list and then the next lesson we will have a deeper look.
To to the list object.
But so for the time being.
This is it.
And the.
Yeah.
Hope you enjoyed it.
And see you in the next session.